/* $Id: TextCustomizer.java,v 1.2 2003/08/02 19:20:02 survey Exp $
 * 
 *                 Sun Public License Notice
 *
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 *
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun
 * Microsystems, Inc. All Rights Reserved.
 */


package org.medbeans.modules.search.types;


import java.awt.Color;
import java.beans.Customizer;
import java.beans.PropertyChangeListener;
import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.CompoundBorder;
import javax.swing.border.TitledBorder;

import org.openide.util.NbBundle;
//import org.openidex.search.SearchType;


/**
 * Customizer of TextType beans.
 *
 * @author  Petr Kuzel

 */
public class TextCustomizer extends JPanel implements Customizer {

    private TextType peer;
    private String lastSubstring = "";
    private String lastRegexp = "";

    /** Creates new form FullTextCustomizer */
    public TextCustomizer() {
        initComponents ();
        initAccessibility ();
        TitledBorder tb = new TitledBorder(getBorderLabel());
        tb.setBorder(new CompoundBorder());
        setBorder (tb);

        ButtonGroup group = new ButtonGroup();
        group.add(substringRadioButton);
        group.add(regexpRadioButton);

    }

    /** Allow derived customizers.
    */
    protected String getBorderLabel() {
        return null;
    }

    private void initAccessibility(){                
        substringTextField.getAccessibleContext().setAccessibleName(NbBundle.getBundle(TextCustomizer.class).getString("ACSN_PROP_SUBSTRING"));
        substringTextField.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(TextCustomizer.class).getString("ACSD_PROP_SUBSTRING"));
        regexpTextField.getAccessibleContext().setAccessibleName(NbBundle.getBundle(TextCustomizer.class).getString("ACSN_PROP_RE"));         
        regexpTextField.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(TextCustomizer.class).getString("ACSD_PROP_RE"));         
        caseSensitiveCheckBox.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(TextCustomizer.class).getString("ACS_TEXT_LABEL_CASE_SENSITIVE"));         
        wholeWordsCheckBox.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(TextCustomizer.class).getString("ACS_TEXT_LABEL_WHOLE_WORDS"));         
        regexpRadioButton.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(TextCustomizer.class).getString("ACS_TEXT_LABEL_RE"));         
        substringRadioButton.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle(TextCustomizer.class).getString("ACS_TEXT_LABEL_SUBSTRING"));         
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the FormEditor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        java.awt.GridBagConstraints gridBagConstraints;

        substringRadioButton = new javax.swing.JRadioButton();
        substringTextField = new javax.swing.JTextField();
        regexpRadioButton = new javax.swing.JRadioButton();
        regexpTextField = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        caseSensitiveCheckBox = new javax.swing.JCheckBox();
        wholeWordsCheckBox = new javax.swing.JCheckBox();

        setLayout(new java.awt.GridBagLayout());

        substringRadioButton.setMnemonic(NbBundle.getBundle(TextCustomizer.class).getString("TEXT_LABEL_SUBSTRING_MNEM").charAt(0));
        substringRadioButton.setSelected(true);
        substringRadioButton.setText(NbBundle.getBundle(TextCustomizer.class).getString("TEXT_LABEL_SUBSTRING"));
        substringRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                substringRadioButtonActionPerformed(evt);
            }
        });

        substringRadioButton.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                substringRadioButtonStateChanged(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 12, 0, 0);
        add(substringRadioButton, gridBagConstraints);

        substringTextField.setColumns(20);
        substringTextField.setPreferredSize(new java.awt.Dimension (100, substringTextField.getPreferredSize ().height));
        substringTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                substringTextFieldKeyReleased(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 12, 0, 11);
        add(substringTextField, gridBagConstraints);

        regexpRadioButton.setMnemonic(NbBundle.getBundle(TextCustomizer.class).getString("TEXT_LABEL_RE_MNEM").charAt(0));
        regexpRadioButton.setText(NbBundle.getBundle(TextCustomizer.class).getString("TEXT_LABEL_RE"));
        regexpRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                regexpRadioButtonActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(12, 12, 5, 0);
        add(regexpRadioButton, gridBagConstraints);

        regexpTextField.setColumns(20);
        regexpTextField.setPreferredSize(new java.awt.Dimension (100, regexpTextField.getPreferredSize ().height));
        regexpTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                regexpTextFieldKeyReleased(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(12, 12, 5, 11);
        add(regexpTextField, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weighty = 1.0;
        add(jPanel1, gridBagConstraints);

        caseSensitiveCheckBox.setMnemonic(NbBundle.getBundle(TextCustomizer.class).getString("TEXT_BUTTON_CASE_SENSITIVE_MNEM").charAt(0));
        caseSensitiveCheckBox.setText(NbBundle.getBundle(TextCustomizer.class).getString("TEXT_LABEL_CASE_SENSITIVE"));
        caseSensitiveCheckBox.setBorder(null);
        caseSensitiveCheckBox.setEnabled(false);
        caseSensitiveCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                caseSensitiveCheckBoxActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 30, 0, 11);
        add(caseSensitiveCheckBox, gridBagConstraints);

        wholeWordsCheckBox.setMnemonic(NbBundle.getBundle(TextCustomizer.class).getString("TEXT_BUTTON_WHOLE_WORDS_MNEM").charAt(0));
        wholeWordsCheckBox.setText(NbBundle.getBundle(TextCustomizer.class).getString("TEXT_LABEL_WHOLE_WORDS"));
        wholeWordsCheckBox.setBorder(null);
        wholeWordsCheckBox.setEnabled(false);
        wholeWordsCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                wholeWordsCheckBoxActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 30, 0, 11);
        add(wholeWordsCheckBox, gridBagConstraints);

    }//GEN-END:initComponents

    private void substringRadioButtonStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_substringRadioButtonStateChanged
        caseSensitiveCheckBox.setEnabled(substringRadioButton.isSelected());
        wholeWordsCheckBox.setEnabled(substringRadioButton.isSelected());
    }//GEN-LAST:event_substringRadioButtonStateChanged

    private void wholeWordsCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_wholeWordsCheckBoxActionPerformed
        peer.setWholeWords(wholeWordsCheckBox.isSelected());
    }//GEN-LAST:event_wholeWordsCheckBoxActionPerformed

    private void caseSensitiveCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_caseSensitiveCheckBoxActionPerformed
        peer.setCaseSensitive(caseSensitiveCheckBox.isSelected());
    }//GEN-LAST:event_caseSensitiveCheckBoxActionPerformed

    private void regexpRadioButtonActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_regexpRadioButtonActionPerformed

        String text = regexpTextField.getText();
        try {
            peer.setRe(text);
        } catch (IllegalArgumentException ex) {
        }

    }//GEN-LAST:event_regexpRadioButtonActionPerformed

    private void substringRadioButtonActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_substringRadioButtonActionPerformed

        peer.setMatchString(substringTextField.getText());

    }//GEN-LAST:event_substringRadioButtonActionPerformed

    private void substringTextFieldKeyReleased (java.awt.event.KeyEvent evt) {//GEN-FIRST:event_substringTextFieldKeyReleased
        String text = substringTextField.getText();
        if (!text.equals(lastSubstring)) {
            peer.setMatchString(text);
            substringRadioButton.setSelected(true);
            lastSubstring = text;
        }
    }//GEN-LAST:event_substringTextFieldKeyReleased

    private void regexpTextFieldKeyReleased (java.awt.event.KeyEvent evt) {//GEN-FIRST:event_regexpTextFieldKeyReleased
        String text = regexpTextField.getText();
        if (!text.equals(lastRegexp)) {
            regexpRadioButton.setSelected(true);
            try {
                peer.setRe(text);
                regexpTextField.setForeground(Color.black);
            } catch (IllegalArgumentException ex) {
                regexpTextField.setForeground(Color.red);
            }
            lastRegexp = text;
        }
    }//GEN-LAST:event_regexpTextFieldKeyReleased

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox caseSensitiveCheckBox;
    private javax.swing.JCheckBox wholeWordsCheckBox;
    private javax.swing.JRadioButton regexpRadioButton;
    private javax.swing.JRadioButton substringRadioButton;
    private javax.swing.JTextField regexpTextField;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField substringTextField;
    // End of variables declaration//GEN-END:variables

    /** Initialize customizer with proper values.
    */
    public void setObject(final Object obj) {

        peer = (TextType) obj;

        // set default coloring
        regexpTextField.setForeground(Color.black);
        substringTextField.setForeground(Color.black);

        /* Display in customizer. */

        substringTextField.setText(peer.getMatchString());
        lastSubstring = peer.getMatchString();
        caseSensitiveCheckBox.setSelected(peer.getCaseSensitive());
        wholeWordsCheckBox.setSelected(peer.getWholeWords());
        regexpTextField.setText(peer.getRe());
        lastRegexp = peer.getRe();

        if(!"".equals(peer.getRe())) // NOI18N
            regexpRadioButton.setSelected(true);
        if(!"".equals(peer.getMatchString())) // NOI18N
            substringRadioButton.setSelected(true);
    }

    public void addPropertyChangeListener(final PropertyChangeListener p1) {
    }

    public void removePropertyChangeListener(final PropertyChangeListener p1) {
    }

    public void requestFocus() {
        JTextField tf = regexpRadioButton.isSelected() ?
                          regexpTextField : substringTextField;
        int n = tf.getText().length();
        if (n > 0) {
            tf.setCaretPosition(0);
            tf.moveCaretPosition(n);
        }
        tf.requestFocus();
    }
}
